package com.prs.crm.action.hr.report;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;

import com.prs.crm.action.ReportAction;
import com.prs.crm.domain.hr.Check;
import com.prs.crm.domain.hr.CheckPlan;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.hr.CheckPlanService;
import com.prs.crm.service.hr.CheckService;
import com.prs.crm.service.hr.PersonService;

public class CheckReportAction extends ReportAction {
	
	private static final long serialVersionUID = 2828873721706755432L;	
	
	private Check check;
    private CheckService  checkService;
    private CheckPlan checkPlan;
    private CheckPlanService  checkPlanService;
	private List<Person> persons;
	private Person person;
	private PersonService personService;
	private String ydm;
	private int counts;
	private String accepts;
	private String acceptnames;
	private String years;
	private String months;
	private String name;
	private PaginationSupport pagination;
    private  List  mothsList;
    private  List weekList;
    private  List nameList;
    private  List checkList;
    private  List datesList;
    private Map<String,Check> mChecks;
    private Map<String,String> personMap;
    private Date currentDate;
    


	public void setPersonMap(Map<String,String> personMap) {
		this.personMap = personMap;
	}

	public Map<String,String> getPersonMap() {
		return personMap;
	}

	public void setmChecks(Map<String,Check> mChecks) {
		this.mChecks = mChecks;
	}

	public Map<String,Check> getmChecks() {
		return mChecks;
	}

	/**
	 * @return
	 */
	@Action("listCheckReport")
	public String listCheckReport(){
		mothsList=new ArrayList();
		datesList=new ArrayList();
		weekList = new ArrayList();
		nameList= new ArrayList();
		checkList= new ArrayList();
		mChecks = new HashMap<String,Check>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		currentDate = new Date();
		
		 ydm=df.format(currentDate);
		 //System.out.println(ydm);
	     this.counts=this.getCount(ydm);
	     //System.out.println(counts);
         persons=this.getPersonService().getAllPersons();
        for(int j=0;j<counts;j++){
        	mothsList.add(j+1);
        	months=ydm+"-"+(((j+1)>9)?(j+1):"0"+(j+1));
        	datesList.add(months);
        	weekList.add(getWeekByYMD(months,"-"));
        	
         }

		for(Person person : persons){
			
    	    	   addChecks(person.getId().toString(),this.getCheckService().getPersonChecksInMonth(person.getId(), ydm));
    	
        }

		return SUCCESS;

	}
	
	/**
	 * 添加考勤记录
	 * @param flag 标志，可以是用户编号或则姓名
	 * @param checks
	 */
	private void addChecks(String flag,Collection<Check> checks)
	{
		if (checks!=null)
		{
			for (Check check :checks)
			{
				mChecks.put(flag+check.getDate(), check);
			}
		}
	}

	@Action(value = "viewReport", results = {
			@Result(name = "success", type = "chain", location = "listCheckReport2"),
			@Result(name = "error", type = "chain", location = "listCheckReprot2") })
     public String viewReport() {
		mothsList=new ArrayList();
		weekList = new ArrayList();
		datesList=new ArrayList();
		checkList= new ArrayList();
		nameList= new ArrayList();
		personMap = new HashMap<String,String>();
    	mChecks = new HashMap<String,Check>();
		String ydm="";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		currentDate = new Date();
		 ydm=df.format(currentDate);
	
		this.counts=this.getCount(ydm);
		
		String[] names=acceptnames.split(",");
		String[] userIds=accepts.split(",");
		for(int i=0;i<names.length;i++){
			 nameList.add(names[i]);
			
		}

		for(int j=0;j<counts;j++){
			 
     	   mothsList.add(j+1);
     	   months=ydm+"-"+(((j+1)>9)?(j+1):"0"+(j+1));
     	   datesList.add(months);
 	
     	   weekList.add(getWeekByYMD(months,"-"));
		}
		
		  for(int h=0;h<nameList.size();h++){
			  personMap.put(userIds[h], names[h]);
			   addChecks(userIds[h],this.getCheckService().getPersonChecksInMonth(Integer.parseInt(userIds[h]), ydm));
			/*   for(int i=0;i<datesList.size();i++){
    	    	   String date=datesList.get(i).toString();
    	    	   addChecks(name,this.getCheckService().getCheckByPersonInMonth(name, date));
			   //Check	check1 =this.getCheckService().getCheckByname(name, date);
			   //checks.add(check1);
			  
			  
		  }*/
			  
		}
		 return SUCCESS;
     }
	
	
	public static String getWeekByYMD(String dateStr,String regix)   
          { 
		
	        String[] arr = dateStr.split(regix); 
	        
	        int uYear = Integer.parseInt(arr[0], 10);   
	       int uMonth = Integer.parseInt(arr[1], 10);   
	        int uDay = Integer.parseInt(arr[2], 10);   
	       int a = (14 - uMonth) / 12;   
	       int y = uYear - a;   
	        int m = uMonth + 12 * a - 2;   
	        int d = (uDay + y + y / 4 - y / 100 + y / 400 + 31 * m / 12) % 7;   
	        String[] week = { "日", "一", "二", "三", "四", "五", "六" };  
	        
	       return week[d];   
	}   
	
	
	public int  getCount(String months){
		int count=0;
       String months1=months.substring(5);

		switch(Integer.parseInt(months1)){
		case 1:
			count=31;
		case 3:
			count=31;
		case 5:
			count=31;
		case 7:
			count=31;
		case 8:
			count=31;
		case 10:
			count=31;
		case 12:
		count=31;
		break;
		case 4:
		case 6:
		case 9:
		case 11:
		count=30;
		break;
		case 2:
		count=28;
		}

		return count;
	}
	
	public Check getCheck() {
		return check;
	}
	public void setCheck(Check check) {
		this.check = check;
	}

	public CheckService getCheckService() {
		return checkService;
	}

	public void setCheckService(CheckService checkService) {
		this.checkService = checkService;
	}

	public CheckPlan getCheckPlan() {
		return checkPlan;
	}

	public void setCheckPlan(CheckPlan checkPlan) {
		this.checkPlan = checkPlan;
	}

	public CheckPlanService getCheckPlanService() {
		return checkPlanService;
	}

	public void setCheckPlanService(CheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

	

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public List<Person> getPersons() {
		return persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public String getYdm() {
		return ydm;
	}

	public void setYdm(String ydm) {
		this.ydm = ydm;
	}

	public String getMonths() {
		return months;
	}

	public void setMonths(String months) {
		this.months = months;
	}

	public int getCounts() {
		return counts;
	}

	public void setCounts(int counts) {
		this.counts = counts;
	}

	public String getAccepts() {
		return accepts;
	}

	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}

	public String getAcceptnames() {
		return acceptnames;
	}

	public void setAcceptnames(String acceptnames) {
		this.acceptnames = acceptnames;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public List getMothsList() {
		return mothsList;
	}

	public void setMothsList(List mothsList) {
		this.mothsList = mothsList;
	}

	public List getWeekList() {
		return weekList;
	}

	public void setWeekList(List weekList) {
		this.weekList = weekList;
	}

	public List getNameList() {
		return nameList;
	}

	public void setNameList(List nameList) {
		this.nameList = nameList;
	}

	public List getCheckList() {
		return checkList;
	}



	public List getDatesList() {
		return datesList;
	}

	public void setDatesList(List datesList) {
		this.datesList = datesList;
	}



	public Date getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	public String getYears() {
		return years;
	}

	public void setYears(String years) {
		this.years = years;
	}

	public void setCheckList(List checkList) {
		this.checkList = checkList;
	}


	
}
